#include "MeshFilter.h"

//////////////////////////////////////////////////////
//Constructor & Destructor
//////////////////////////////////////////////////////

MeshFilter::MeshFilter()
{
	_vertex = 0;
	_normals = 0;
	_index = NULL;
}

MeshFilter::~MeshFilter()
{
	/*
	if(_vertex != NULL)
		delete _vertex;

	if(_normals != NULL)
		delete _normals;

	if(_index != NULL)
		delete _index;
*/

	glDeleteBuffers(1, &_indexVBO);
	glDeleteBuffers(1, &_normalsVBO);
	glDeleteBuffers(1, &_vertexVBO);
}


//////////////////////////////////////////////////////
//Operations
//////////////////////////////////////////////////////

//Apply vertex, normals & index in vbo
void MeshFilter::Apply()
{
	if(_vertex == 0 || _index == 0)
		return;

	glGenBuffers(1, &_vertexVBO);
	glBindBuffer(GL_ARRAY_BUFFER, _vertexVBO);
	glBufferData(GL_ARRAY_BUFFER, 3*sizeof(GLfloat)*_vertexCount, _vertex, GL_STATIC_DRAW);

	if(_normals != 0)
	{
		glGenBuffers(1, &_normalsVBO);
		glBindBuffer(GL_ARRAY_BUFFER, _normalsVBO);
		glBufferData(GL_ARRAY_BUFFER, 3*sizeof(GLfloat)*_vertexCount, _normals, GL_STATIC_DRAW);
	}
	
	glGenBuffers(1, &_indexVBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(short)*_indexCount, _index, GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}